﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TKFA.Model;
using C1.Web.Wijmo.Controls.C1GridView;
using System.Data;
using System.Collections;
using System.Text;

namespace FixedAssets.Module.ManagementProcess
{
    public partial class FA_BuyPlan : System.Web.UI.Page
    {
        /// <summary>
        /// 
        /// </summary>
        List<FA_PROPERTY_PAGEITEM> lpp;
        FA_PURCHASEPLAN pplan;
        FA_PURCHASEVALUE pvalue;
       static DataTable planvalue_dt;
        protected void Page_Load(object sender, EventArgs e)
        {
            //if (!IsPostBack)
            //{
            //    TKFA.BLL.FA_PROPERTY_PAGEITEM p=new TKFA.BLL.FA_PROPERTY_PAGEITEM ();
            //    lpp = p.GetModelList(" SCORPCODE='JW00000001' AND SPAGEITEMNAME='设备购置计划'");
            //    C1GridView_ColumnBinding(lpp, this.C1GridView1);
            //    ADD_C1GridView_NullRow(this.C1GridView1);
            //}
            //this.C1GridView1.RowUpdating += new C1GridViewUpdateEventHandler(this.C1GridView1_RowUpdating);

            if (!IsPostBack) 
            {
                TKFA.BLL.FA_PROPERTY_PAGEITEM p = new TKFA.BLL.FA_PROPERTY_PAGEITEM();
                lpp = p.GetModelList(" SCORPCODE='JW00000001' AND SPAGEITEMNAME='设备购置计划'");
                grid_hid.Value = listtojson(lpp);

                string str_col = "ID";
                foreach (FA_PROPERTY_PAGEITEM pp in lpp)
                {
                    str_col += "," + pp.SCODE;                    
                }
                col_hid.Value = "["+str_col+"]";
            }


        }

        protected void btnSave_Click(object sender, EventArgs e)
        {

        }

        protected void btnCancle_Click(object sender, EventArgs e)
        {

        }

        protected void datebut_Click(object sender, EventArgs e)
        {
            //if (this.Calendar1.Visible)
            //    this.Calendar1.Visible = false;
            //else
            //    this.Calendar1.Visible = true;
        }

        protected void Calendar1_SelectionChanged(object sender, EventArgs e)
        {
            //this.TextBox2.Text = this.Calendar1.SelectedDate.ToString("yyyy/MM/dd");
            //this.Calendar1.Visible = false;
        }

        private void C1GridView_ColumnBinding(List<FA_PROPERTY_PAGEITEM> lproperty, C1GridView c1gridview)
        {
            planvalue_dt = new DataTable();
            planvalue_dt.Columns.Clear();
            planvalue_dt.Columns.Add("GUID",typeof(string));
            foreach (FA_PROPERTY_PAGEITEM p in lproperty)
            {
                C1BoundField f = new C1BoundField();
                f.HeaderText = p.SNAME;
                f.DataField = p.SCODE;
                c1gridview.Columns.Add(f);
                DataColumn dc = new DataColumn();
                dc.ColumnName = p.SCODE;
                dc.Caption = p.SNAME;
                dc.DataType = typeof(string);
                planvalue_dt.Columns.Add(dc);
            }
            planvalue_dt.PrimaryKey=new DataColumn[]{planvalue_dt.Columns[0]};
        }

        private void ADD_C1GridView_NullRow(C1GridView c1gridview)
        {
            DataRow dr = planvalue_dt.NewRow();
            dr[0] = System.Guid.NewGuid().ToString();
            planvalue_dt.Rows.Add(dr);
            c1gridview.DataSource = planvalue_dt;
            c1gridview.DataBind();
            c1gridview.DataKeyNames = new string[] { "GUID" };
            int i=1;
            foreach (C1GridViewRow r in c1gridview.Rows)
            {
                r.Cells[1].Text = i.ToString();
                i++;
            }
            c1gridview.Rows[planvalue_dt.Rows.Count - 1].Cells[1].Text = "*";
        }

        private void DEL_C1GridView_Row(C1GridView c1gridview,string clientid)
        {
            foreach (C1GridViewRow r in c1gridview.Rows)
            {
                if (r.Cells[0].FindControl("butDelete").ClientID.Equals(clientid))
                {
                    if (r.Cells[1].Text.Equals("*"))
                    {

                    }
                    else
                    {
                        planvalue_dt.Rows.RemoveAt(int.Parse(r.Cells[1].Text));
                    }
                }
            }
            c1gridview.DataSource = planvalue_dt;
            c1gridview.DataBind();
            c1gridview.DataKeyNames = new string[] { "GUID" };
            int i = 1;
            foreach (C1GridViewRow r in c1gridview.Rows)
            {
                r.Cells[1].Text = i.ToString();
                i++;
            }
            c1gridview.Rows[planvalue_dt.Rows.Count - 1].Cells[1].Text = "*";
        }
        
        protected void butAdd_Click(object sender, EventArgs e)
        {
            
        }

        protected void butDelete_Click(object sender, EventArgs e)
        {
           
        }

        private string listtojson(List<FA_PROPERTY_PAGEITEM> lpage)
        {
            StringBuilder strb = new StringBuilder();
            strb.Append("[");
            int index = 1;
            strb.Append("{dataKey:\"ID\",headerText:\"序号\",dataType:\"number\", dataFormatString:\"n0\"},");
            foreach (FA_PROPERTY_PAGEITEM p in lpage)
            {
                strb.Append("{dataKey:\"" + p.SCODE + "\",headerText:\"" + p.SNAME + "\",dataType:\"string\"}");
                if (index != lpage.Count)
                {
                    strb.Append(",");
                    index++;
                }
            }
            strb.Append(",{dataKey:\"CZ\",headerText:\"操作\",buttonType:\"button\"}");
            strb.Append("]");
            return strb.ToString();
        
        }

       
    }
}